﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
using UnityEditor;

namespace SLG_Demo_0
{
	/// <summary>
	/// 完整的地图对象，包含数据层和显示层。一个 MapObject 表示一张地图
	/// </summary>
	public class MapObject : MonoBehaviour
	{
		/// <summary>
		/// 地图ID
		/// </summary>
		private int m_MapID;

		/// <summary>
		/// 地图数据层
		/// </summary>
		private MapData m_MapData = null;

		/// <summary>
		/// 地图显示层
		/// </summary>
		public MapDisplay m_MapDisplay = null;

		/// <summary>
		/// 地图宽高
		/// </summary>
		public int m_width = 0, m_height = 0;

		/// <summary>
		/// 需要指定的 UnityEngine.Tilemaps.Tilemap 地图对象
		/// </summary>
		public Tilemap m_TileMap = null;

		/// <summary>
		/// 需要指定的，当前地图格子显示用的精灵
		/// </summary>
		public Sprite m_Sprite = null;

		/// <summary>
		/// 是否显示寻路路径
		/// </summary>
		public bool m_bShowSearch = false;

		////////////////////////////////////////////////////////////
		public MapData MapData
		{
			get
			{
				return m_MapData;
			}

			set
			{
				m_MapData = value;
			}
		}

		public MapDisplay MapDisplay
		{
			get
			{
				return m_MapDisplay;
			}

			set
			{
				m_MapDisplay = value;
			}
		}

		public int MapID
		{
			get
			{
				return m_MapID;
			}

			set
			{
				m_MapID = value;
			}
		}


		////////////////////////////////////////////////////////////
		public void CreateMap()
		{
			// 创建数据层
			m_MapData = new MapData();
			m_MapData.Create(this, m_width, m_height);

			// 创建显示层
			m_MapDisplay = new MapDisplay(m_TileMap, m_Sprite);
			m_MapDisplay.Create(this, m_width, m_height);



			// 测试地图：创建
			/*m_MapData = new MapData();
			m_MapData.Create(this, 8, 8);
			m_MapDisplay = new MapDisplay(m_TileMap, m_Sprite);
			m_MapDisplay.Create(this, 8, 8);*/
			RefreshInitMap();
		}

		// 重新刷新地图颜色和数据
		public void RefreshInitMap()
		{
			// 测试地图：设置地形
			int[,] swampAxis = new int[,]
			{
				{0, 2},
				{3, 3},
			};
			int[,] obstacleAxis = new int[,]
			{
				//{1, 0},
				{7, 7},
			};

			for (int iSwamp = 0; iSwamp < swampAxis.GetLength(0); ++iSwamp)
			{
				int x = swampAxis[iSwamp, 0];
				int y = swampAxis[iSwamp, 1];

				m_MapData.Cells[x, y].DataType = ECellDataType.Swamp;
				m_MapDisplay.m_Cells[x, y].DisplayType = ECellDisplayType.Swamp;
			}

			for (int iObstacle = 0; iObstacle < obstacleAxis.GetLength(0); ++iObstacle)
			{
				int x = obstacleAxis[iObstacle, 0];
				int y = obstacleAxis[iObstacle, 1];

				m_MapData.Cells[x, y].DataType = ECellDataType.Obstacle;
				m_MapDisplay.m_Cells[x, y].DisplayType = ECellDisplayType.Obstacle;
			}
		}

		public void DestroyMap()
		{
			//
		}

		void Start()
		{
			CreateMap();

			// 创建测试单位预设体
			//GameObject testUnit_0 = AssetDatabase.LoadAssetAtPath<GameObject>(GameConstVar.PrefabPath + "/TestUnit_0.prefab");
			GameObject testUnit_0 = Resources.Load<GameObject>("Prefabs/TestUnit_0");
			if (testUnit_0 != null)
			{
				Vector3 pos = m_MapDisplay.CellToWorld(2, 3);
				Instantiate(testUnit_0, pos, Quaternion.identity);
			}

			//GameObject testUnit_1 = AssetDatabase.LoadAssetAtPath<GameObject>(GameConstVar.PrefabPath + "/TestUnit_0.prefab");
			GameObject testUnit_1 = Resources.Load<GameObject>("Prefabs/TestUnit_0");
			if (testUnit_1 != null)
			{
				Vector3 pos = m_MapDisplay.CellToWorld(0, 0);
				Instantiate(testUnit_1, pos, Quaternion.identity);
			}
		}

		void Update()
		{
			if (m_MapDisplay != null)
			{
				m_MapDisplay.Update();
			}
		}
	}
}
